<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>夏小蕾作业</title>
    <link rel="stylesheet" href="./frame/easyui/themes/bootstrap/easyui.css">
    <link rel="stylesheet" href="./frame/easyui/themes/icon.css">
    <link rel="stylesheet" href="./frame/bootstrap/css/bootstrap.min.css">
</head>

<body>
    <style>
        #tableUser{
            width:600px;
        }
        #ff{
            width:600px;
        }
    </style>
    <h3>Tree插件使用</h3>
    <ul id="tt" class="easyui-tree"></ul>
    <h3>datagrid原生使用</h3>
    <div id='ff'>
    <table id='tableUser'></table>
    </div>
    <!--<button id='btnLoad'>load</button>-->
    <h3>datagrid插件使用</h3>
    <table id='tableUser' class='table'>
        <thead>
            <th>用户名</th>
            <th>年龄</th>
            <th>邮箱</th>
            <th>手机号码</th>
        </thead>
        <tbody>
        </tbody>
    </table>

    <script src='./frame/jquery-3.3.1.min.js'></script>
    <script src='./frame/easyui/jquery.easyui.min.js'></script>
    <script>
        // datagrid原生使用
        $('#tableUser').datagrid({
            url: '/user/list',
            method: 'get',
            loadFilter: function (result) {
                return result.data
            },
            columns: [[
                { field: 'username', title: '用户名', width: '100' },
                { field: 'age', title: '年龄', width: '80' },
                { field: 'email', title: '邮箱', width: '180' },
                { field: 'phone', title: '手机' },
            ]],
        })
        //datagrid插件使用
        function createRow(row) {
            var tr = $('<tr></tr>')
            var td = $('<td></td>')
            $.each(row, function (key, value) {
                var td = $('<td></td>').html(value)
                tr.append(td)
            })
            return tr
        }
        $.get('/user/list').done(function (result) {
            $(result.data).each(function (index, value) {
                $('#tableUser tbody').append(createRow(value))
            })
        })
        //tree的使用方法
        $('#tt').tree({
            url: '/user/tree',
            method: 'get',
            checkbox: true,
            loadFilter: function (result) {
                return format(result.data)
            }
        })
        function format(data, pid = 0, res = []) {
            data.forEach(function (row) {
                if (row.pid == pid) {
                    row.children = [],
                        res.push(row)
                    format(data, row.id, row.children)
                }
            })
            return res
        }
    </script>
</body>

</html>